import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: AB路线
 * User: DELL
 * Date: 2024-05-23
 * Time: 18:11
 */
public class Main7 {

    static int[] dx = {0, 0, 1, -1};
    static int[] dy = {1, -1, 0, 0};

    static class Node {
        int x;
        int y;

        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int K = scanner.nextInt();
        char[][] map = new char[n][m];
        for (int i = 0; i < n; i++) {
            String s = scanner.next();
            for (int j = 0; j < m; j++) {
                map[i][j] = s.charAt(j);
            }
        }
        boolean[][][] visited = new boolean[n][m][K + 1];
        visited[0][0][1] = true;
        Queue<Node> queue = new LinkedList<>();
        queue.add(new Node(0, 0));
        long ans = 0;
        int k = 0;
        while (!queue.isEmpty()) {
            ans++;
            k++;
            k %= K;
            int size = queue.size();
            while (size-- > 0) {
                Node node = queue.poll();
                if (node.x == n - 1 && node.y == m - 1) {
                    System.out.println(ans - 1);
                    return;
                }
                char ch = map[node.x][node.y];
                for (int i = 0; i < 4; i++) {
                    int x = node.x + dx[i];
                    int y = node.y + dy[i];
                    if (!(x >= 0 && x < n && y >= 0 && y < m)) {
                        continue;
                    }
                    // 需要换字母
                    if (k == 0 && map[x][y] != ch && !visited[x][y][k + 1]) {
                        visited[x][y][k + 1] = true;
                        queue.add(new Node(x, y));
                    }
                    // 不需要换字母
                    if (k != 0 && map[x][y] == ch && !visited[x][y][k + 1]) {
                        visited[x][y][k + 1] = true;
                        queue.add(new Node(x, y));
                    }
                }
            }
        }
        System.out.println(-1);
    }
}
